/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.inria.openmath.omapi.implementation;

/**
 *
 * @author Mr.Sam
 */

import java.io.*;
import java.math.BigInteger;
import fr.inria.openmath.omapi.*;

/**
 * This is a sample implementation of a ParserHamdler.
 * <p> This one traces incoming events and forwards them to another
 * ParserHandler (if set).
 * It may thus be used as a debuging tool.
 *
 * @see fr.inria.openmath.omapi.ParserHandler
 */
public class DebugParserHandler extends ParserAdapter {

    /** If not null then events are forwarded to this ParserHandler */
    ParserHandler forward;

    /** Where to print trace */
    PrintWriter pw;

    private String toHexString(byte[] value) {
        int i, LSNib, MSNib, l = value.length;
        char s[] = new char[l * 2];

        for (i = 0; i < l; i++) {
            MSNib = (value[i] & 0xf0) >> 4;
            LSNib = (value[i] & 0x0f);
            if (MSNib < 10)
                s[2 * i] = (char) ('0' + MSNib);
            else
                s[2 * i] = (char) ('a' + MSNib - 10);
            if (LSNib < 10)
                s[2 * i + 1] = (char) ('0' + LSNib);
            else
                s[2 * i + 1] = (char) ('a' + LSNib - 10);
        }
        return new String(s);
    }


    /**
     * Creates a new DebugParserHandler that will forward events.
     * @param forward The ParserHandler to forward events to. (trace them then froward them)
     * @param pw Where to print trace.
     */
    public DebugParserHandler (ParserHandler forward, PrintWriter pw) {
        this.forward = forward;
        this.pw = pw;
    }

    /**
     * Creates a new DebugParserHandler that will forward events.
     * @param forward The ParserHandler to forward events to. (trace them then froward them)
     */
    public DebugParserHandler (ParserHandler forward) {
        this(forward,
                new PrintWriter(new OutputStreamWriter(System.out), true));
    }

    /**
     * Creates a new DebugParserHandler that wont forward events.
     * Trace is done on default output (System.out).
     */
    public DebugParserHandler () {
        this((ParserHandler) null,
                new PrintWriter(new OutputStreamWriter(System.out), true));
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#startParse
     */
    public void startParse() throws OMException {
        pw.println("startParse");
        if (forward != null) {
            forward.startParse();
        }
    }

    /**
     * @see fr.inria.openmath.omapi.ParserHandler#endParse
     */
    public void endParse() throws OMException {
        pw.println("endParse");
        if (forward != null) {
            forward.endParse();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#startObject
     */
    public void startObject() throws OMException {
        pw.println("startObject");
        if (forward != null) {
            forward.startObject();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#endObject
     */
    public void endObject() throws OMException {
        pw.println("endObject");
        if (forward != null) {
            forward.endObject();
        }
    }



    /**
     * @see fr.inria.openmath.omapi.ParserHandler#startApplication
     */
    public void startApplication() throws OMException {
        pw.println("startApplication");
        if (forward != null) {
            forward.startApplication();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#endApplication
     */
    public void endApplication() throws OMException {
        pw.println("endApplication");
        if (forward != null) {
            forward.endApplication();
        }
    }



    /**
     * @see fr.inria.openmath.omapi.ParserHandler#startAttribution
     */
    public void startAttribution() throws OMException {
        pw.println("startAttribution");
        if (forward != null) {
            forward.startAttribution();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#endAttribution
     */
    public void endAttribution() throws OMException {
        pw.println("endAttribution");
        if (forward != null) {
            forward.endAttribution();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#startAttributePairs
     */
    public void startAttributePairs() throws OMException {
        pw.println("startAttributePairs");
        if (forward != null) {
            forward.startAttributePairs();
        }
    }



    /**
     * @see fr.inria.openmath.omapi.ParserHandler#endAttributePairs
     */
    public void endAttributePairs() throws OMException {
        pw.println("endAttributePairs");
        if (forward != null) {
            forward.endAttributePairs();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#startBind
     */
    public void startBind() throws OMException {
        pw.println("startBind");
        if (forward != null) {
            forward.startBind();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#endBind
     */
    public void endBind() throws OMException {
        pw.println("endBind");
        if (forward != null) {
            forward.endBind();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#startBVars
     */
    public void startBVars() throws OMException {
        pw.println("startBVars");
        if (forward != null) {
            forward.startBVars();
        }
    }



    /**
     * @see fr.inria.openmath.omapi.ParserHandler#endBVars
     */
    public void endBVars() throws OMException {
        pw.println("endBVars");
        if (forward != null) {
            forward.endBVars();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#startError
     */
    public void startError() throws OMException {
        pw.println("startError");
        if (forward != null) {
            forward.startError();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#endError
     */
    public void endError() throws OMException {
        pw.println("endError");
        if (forward != null) {
            forward.endError();
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readInteger
     */
    public void readInteger(int value) throws OMException {
        pw.println("readInteger \"" + value + "\"");
        if (forward != null) {
            forward.readInteger(value);
        }
    }

    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readInteger
     */
    public void readInteger(BigInteger value) throws OMException {
        pw.println("readInteger \"" + value + "\"");
        if (forward != null) {
            forward.readInteger(value);
        }
    }

    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readFloat
     */
    public void readFloat(double value) throws OMException {
        pw.println("readFloat \"" + value + "\"");
        if (forward != null) {
            forward.readFloat(value);
        }
    }

    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readString
     */
    public void readString(String value) throws OMException {
        pw.println("readString \"" + value + "\"");
        if (forward != null) {
            forward.readString(value);
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readVariable
     */
    public void readVariable(String name) throws OMException {
        pw.println("readVariable \"" + name + "\"");
        if (forward != null) {
            forward.readVariable(name);
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readByteArray
     */
    public void readByteArray(byte[] value) throws OMException {
        pw.println("readByteArray \"" + toHexString(value) + "\"");
        if (forward != null) {
            forward.readByteArray(value);
        }
    }


    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readSymbol
     */
    public void readSymbol(Symbol value) throws OMException {
        pw.println("readSymbol \"" + value + "\"");
        if (forward != null) {
            forward.readSymbol(value);
        }
    }

    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readComment
     */
    public void readComment(String comment) throws OMException {
        pw.println("comment \"" + comment + "\"");
        if (forward != null) {
            forward.readComment(comment);
        }
    }

    /**
     * @see fr.inria.openmath.omapi.ParserHandler#readPInstruction
     */
    public void readPInstruction(String pi) throws OMException {
        pw.println("processing instruction \"" + pi + "\"");
        if (forward != null) {
            forward.readPInstruction(pi);
        }
    }

}
